<?php namespace Home\Controller; 

use Hdphp\Controller\Controller;

//购物车结算控制器(用的是cart表)
class PayController extends Controller{
	private $model;
	private $uid = null;   //这里得进行私有
	public function __init(){
		$this->model = new \Common\Model\Cart;
		if(isset($_SESSION['uinfo']['uid'])){
			$this->uid = $_SESSION['uinfo']['uid'];
			// p($uid);
		}
	}
	/**
	 * 购物车结算显示页
	 */
	public function index(){
		$uid = $this->uid;
		$CartData = $this->model->show($uid);//获取对应用户的购物车数据
		$PayTotal = 0;
		foreach ($CartData as $key => $value) {
			$PayTotal += $value['subtotal'];
		}
		$this->assign('CartData',$CartData);
		$this->assign('PayTotal',$PayTotal);
		// p($CartData);	
		$this->display();
	}

	/**
	 * 异步更新数据库数据
	 */
	public function ajaxUpdata(){
		// p($_POST);接收到页面传过来的数据后，得更新数据库数据
		// 得在这儿将条件都给组合好，然后直接在cart那儿进行更新就行
		$num = $_POST['num'];
		$uid = $_SESSION['uinfo']['uid'];
		$where = array(  //组合的条件
			'crid' =>$_POST['crid'],
			'id'=>$_POST['gid'],
			'uid'=>$_SESSION['uinfo']['uid']
		);
		// p($where);
		$data = array(  //组合需要更新的数据
			'crid' =>$_POST['crid'],
			'id'=>$_POST['gid'],
			'uid'=>$_SESSION['uinfo']['uid'],
			'num'=>$_POST['num'],
		);
		// p($data);
		if($this->model->save($data)){
			$CartData = $this->model->show($uid);//获取对应用户的购物车数据
			$PayTotal = 0;
			foreach ($CartData as $key => $value) {
				$PayTotal += $value['subtotal'];
			}
			$result = array('status'=>true,'num'=>$num,'price'=>$_POST['price'],'PayTotal'=>$PayTotal);
		}
		exit(json_encode($result));
	}
	public function Loaction(){
		// p($_POST);
		$id=$_POST;
		$result = array('status'=>true,'id'=>$id);
		exit(json_encode($result));
	}
}
?>